package com.lance.answer.gui.answer.gk.answer;

import com.alibaba.fastjson.JSONObject;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import com.lance.answer.gui.answer.AnswerObject;
import com.lance.answer.gui.answer.Util;
import com.lance.answer.gui.answer.gk.DealInfoUtil;
import com.lance.answer.gui.answer.gk.answer.task.CreateTopicTask;
import com.lance.answer.gui.answer.gk.answer.task.DoAnswerTask;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.*;
import java.util.concurrent.LinkedBlockingDeque;

@Slf4j
public class DoCreateTopic {

    public static void main(String[] args) throws Exception {

        int sync = 14;


        LinkedBlockingDeque<AnswerDTO> answerQueue = new LinkedBlockingDeque<>();

        List<String> teachers = Lists.newArrayList();

        teachers.add("刘庆");
        teachers.add("李明");
        teachers.add("刘");
        teachers.add("李俊杰");
        teachers.add("艾");
        teachers.add("宋柳萌");
        teachers.add("朱晓玉");
        teachers.add("零散");

        for(String teacher:teachers){
            // 获取所有的用户名和密码
            InputStream userInputStream = new FileInputStream("libs//teachers//"+teacher+".user");
            List<String> userString = IOUtils.readLines(userInputStream, "utf-8");
            Map<String, String> usernameAndPwd = Maps.newHashMap();
            for (String user : userString) {
                String[] strs = user.split(",");
                String userCode = strs[0];
                String pwd = "";
                if (strs[1].length() < 18) {
                    pwd = strs[1];
                } else {
                    pwd = strs[1].substring(6, 14);
                    pwd = "Ouchn" + pwd;
                }
                usernameAndPwd.put(userCode, pwd);
            }

            Map<String, Set<String>> forumMap = Maps.newHashMap();
            for(File file:new File("libs//courseUrlInfo").listFiles()){

                Set<String> forums = Sets.newHashSet();
                List<String> urlList = IOUtils.readLines(new FileInputStream(file),"UTF-8");
                for(String url:urlList){
                    if(url.contains("forum")){
                        //     if(url.contains("形考")||url.contains("作业")){
                        if(url.contains("形考")
                                ||url.contains("测验")
                                ||!url.contains("考试")
                                ||url.contains("专题学习活动")
                                ||url.contains("计分")
                                ||!url.contains("分")
                        ){
                            forums.add(url);
                        }
                    }
                }
                String name = file.getName();
                name = name.substring(0,name.length()-4);
                forumMap.put(name,forums);
            }


            // key = subjectName value 做题链接集合
            Map<String,  Set<String> > mapBySubjectName = Maps.newHashMap();
            //      mapBySubjectName.put("政府经济学",Sets.newHashSet("http://xinjiang.ouchn.cn/mod/forum/view.php?id=310901"));
            for(File f1: Objects.requireNonNull(new File("libs//courseUrlInfo").listFiles())){
                Set<String> urlTaskSet = Sets.newHashSet();
                if(f1.length()<100){
                    continue;
                }
                String fileName = f1.getName();
                String subjectName = fileName.substring(0,fileName.length()-4);
                List<String> urlInfoList = IOUtils.readLines(new FileInputStream(f1), "utf-8");
                for(String urlInfo:urlInfoList){
                    if(!urlInfo.contains("forum")){
                        continue;
                    }

                    if(!urlInfo.contains("形考")
                            &&!urlInfo.contains("测验")
                            &&!urlInfo.contains("考试")
                            &&!urlInfo.contains("专题学习活动")
                            &&!urlInfo.contains("计分")
                            &&!urlInfo.contains("分")
                    ){
                        continue;
                    }
                    System.err.println("urlInfo:"+urlInfo);
                    String urlTask = urlInfo.split(",")[0];
                    urlTaskSet.add(urlTask);
                }
                mapBySubjectName.put(subjectName,urlTaskSet);
            }

            //    Thread.sleep(2000000);

            for(String subjectName:mapBySubjectName.keySet()){


                // 过滤掉英语
                if(Strings.isNullOrEmpty(subjectName)||subjectName.contains("英语")){
                    continue;
                }
                if(mapBySubjectName.get(subjectName).size()<1){
                    continue;
                }
                if(!subjectName.equals("西方行政学说")){
                 continue;
                }
                log.info("subjectName:{}",subjectName);

                //





                List<String> userNames = DealInfoUtil.countUserCourseInfo("libs//userCourseInfo//" + teacher + "//").get(subjectName);
                if(userNames == null){
                    continue;
                }
                for(String username:userNames){
//               if(!Util.getVip().contains(username)){
//                   continue;
//               }

                    String pwd = usernameAndPwd.get(username);
                    AnswerDTO answerDTO = new AnswerDTO();
                    answerDTO.setSubjectName(subjectName);
                    answerDTO.setUsername(username);
                    answerDTO.setTopicUrl(Lists.newLinkedList(forumMap.get(subjectName)));
                    answerDTO.setPwd(pwd);
                    answerDTO.setCommit(true);
                    answerDTO.setPush(false);
                    //      System.err.println("Lists.newLinkedList(mapBySubjectName.get(subjectName))"+Lists.newLinkedList(mapBySubjectName.get(subjectName))+"   "+subjectName);
                    answerDTO.setTopicUrl(Lists.newLinkedList(mapBySubjectName.get(subjectName)));
//                    if(!answerDTO.getSubjectName().equals("社会调查研究与方法")){
//                        continue;
//                    }
                    //    System.err.println("answerDTO:"+answerDTO);
                    answerQueue.add(answerDTO);
                }

            }

        }












        for (int i = 1; i <= sync; i++) {
            Thread answerThread = new CreateTopicTask(null,answerQueue);
            //log.info("加载课程ID：{} 名称：{} ", openCourseId,subjectName);
            answerThread.setName( "答题线程--" + i);
            answerThread.start();
        }


    }




}
